package com.example.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.pojo.Admin;
import com.example.common.pojo.AdminRole;
import com.example.common.pojo.Role;
import com.example.server.security.context.SecurityContextHolder;
import com.example.server.service.IAdminRoleService;
import com.example.server.service.IAdminService;
import com.example.server.service.IRoleService;
import com.example.common.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 小红
 * @since 2022-09-02
 */
@RestController
public class AdminController {

    @Autowired
    private IAdminService adminService;
    @Autowired
    private IRoleService iRoleService;
    @Autowired
    private IAdminRoleService iAdminRoleService;
    @Autowired
    private SecurityContextHolder securityContextHolder;

    @PostMapping("/login")
    public RespBean login(@RequestBody Admin admin, HttpSession session) {

        /**
         * 1 搜集客户端得数据
         * 1.1 数据校验
         * 2 将数据传递给 service，执行业务逻辑
         * 3 将service处理得结果封装为RespBean，发送给客户端
         */
        RespBean respBean = adminService.login(admin, session);

        return respBean;
    }

    @GetMapping("/admin/info")
    public Admin adminInfo() {
        Admin userDetails = (Admin) securityContextHolder.getUserDetails();
        return userDetails;
    }

    @GetMapping("/system/admin/")
    public List<Admin> ListAdmins(String keywords) {
        List<Admin> adminsByKeywords = adminService.getAdminsByKeywords(keywords);
        return adminsByKeywords;
    }
    @PutMapping("/system/admin/")
    public RespBean editAdmin(@RequestBody Admin admin){
        adminService.updateById(admin);
        return RespBean.success();
    }
    @DeleteMapping("/system/admin/{id}")
    public RespBean deleteAdmin(@PathVariable Integer id){
        adminService.removeById(id);
        return RespBean.success();
    }
    @GetMapping("/system/admin/roles")
    public List<Role> roleList(){
        List<Role> list = iRoleService.list();
        return list;
    }
    @PutMapping("/system/admin/roles")
    public RespBean editAdminRole(Integer adminId,Integer[] rids){
        return iAdminRoleService.editAdminRoles(adminId,rids);
    }

    @GetMapping("/system/admin/{adminId}/rids")
    public List<Integer> listRidsByAdmin(@PathVariable Integer adminId){
        List<AdminRole> list = iAdminRoleService.list(new QueryWrapper<AdminRole>().eq("adminId",adminId));
        List<Integer> collect = list.stream()
                .map(AdminRole::getRid)
                .collect(Collectors.toList());
        return collect;
    }
    @GetMapping("/system/basic/admin/")
    public List<Admin> adminList() {
//        List<Admin> list = adminService.list();
//        return list;
        List<Admin> list = adminService.getAdmin();
        return list;
    }

}
